<?php
/**
 * Created by PhpStorm.
 * Author: dt <******@**.com>
 * Date: 2018-10-10
 */

/* *
 * * *****************************************************************
 * * ***** 工具函数 常用函数
 * * ************************************************************
* */

/**
 * 检测姓名
 * @param  integer $name 姓名
 * @return boolean     检测结果
 * @author
 */
function checkRealName($name){
    if (!$name){
        return false;
    }
    $rule = '/^[\x{4e00}-\x{9fa5}]{2,10}$/isu';
    preg_match($rule,$name,$result);
    return $result ? !!($name === $result[0]) : false;
}

/**
 * 检测身份证号
 * @param  integer $cardid 身份证号
 * @return boolean     检测结果
 * @author
 */
function checkCardId($cardid){
    if (!$cardid){
        return false;
    }
    $rule = '/^\d{15}$)|(^\d{17}([0-9]|X)$/isu';
    preg_match($rule,$cardid,$result);
    return $result ? !!($cardid === $result[0]) : false;
}

//验证身份证是否有效
function validateIDCard($IDCard) {
    if (strlen($IDCard) == 18) {
        return check18IDCard($IDCard);
    } elseif ((strlen($IDCard) == 15)) {
        $IDCard = convertIDCard15to18($IDCard);
        return check18IDCard($IDCard);
    } else {
        return false;
    }
}

//计算身份证的最后一位验证码,根据国家标准GB 11643-1999
function calcIDCardCode($IDCardBody) {
    if (strlen($IDCardBody) != 17) {
        return false;
    }

    //加权因子
    $factor = array(7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2);
    //校验码对应值
    $code = array('1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2');
    $checksum = 0;

    for ($i = 0; $i < strlen($IDCardBody); $i++) {
        $checksum += substr($IDCardBody, $i, 1) * $factor[$i];
    }

    return $code[$checksum % 11];
}

// 将15位身份证升级到18位
function convertIDCard15to18($IDCard) {
    if (strlen($IDCard) != 15) {
        return false;
    } else {
        // 如果身份证顺序码是996 997 998 999，这些是为百岁以上老人的特殊编码
        if (array_search(substr($IDCard, 12, 3), array('996', '997', '998', '999')) !== false) {
            $IDCard = substr($IDCard, 0, 6) . '18' . substr($IDCard, 6, 9);
        } else {
            $IDCard = substr($IDCard, 0, 6) . '19' . substr($IDCard, 6, 9);
        }
    }
    $IDCard = $IDCard . calcIDCardCode($IDCard);
    return $IDCard;
}

// 18位身份证校验码有效性检查
function check18IDCard($IDCard) {
    if (strlen($IDCard) != 18) {
        return false;
    }

    $IDCardBody = substr($IDCard, 0, 17); //身份证主体
    $IDCardCode = strtoupper(substr($IDCard, 17, 1)); //身份证最后一位的验证码

    if (calcIDCardCode($IDCardBody) != $IDCardCode) {
        return false;
    } else {
        return true;
    }
}

// 过滤文本
function filteText($str){
    if (empty($str)) return '';
    $str = htmlspecialchars($str);
    $str = str_replace( '/', "", $str);
    $str = str_replace( '"', "", $str);
    $str = str_replace( '(', "", $str);
    $str = str_replace( ')', "", $str);
    $str = str_replace( 'CR', "", $str);
    $str = str_replace( 'ASCII', "", $str);
    $str = str_replace( 'ASCII 0x0d', "", $str);
    $str = str_replace( 'LF', "", $str);
    $str = str_replace( 'ASCII 0x0a', "", $str);
    $str = str_replace( ',', "", $str);
    $str = str_replace( '%', "", $str);
    $str = str_replace( ';', "", $str);
    $str = str_replace( 'eval', "", $str);
    $str = str_replace( 'open', "", $str);
    $str = str_replace( 'sysopen', "", $str);
    $str = str_replace( 'system', "", $str);
    $str = str_replace( '$', "", $str);
    $str = str_replace( "'", "", $str);
    $str = str_replace( "'", "", $str);
    $str = str_replace( 'ASCII 0x08', "", $str);
    $str = str_replace( '"', "", $str);
    $str = str_replace( '"', "", $str);
    $str = str_replace("", "", $str);
    $str = str_replace("&gt", "", $str);
    $str = str_replace("&lt", "", $str);
    $str = str_replace("<SCRIPT>", "", $str);
    $str = str_replace("</SCRIPT>", "", $str);
    $str = str_replace("<script>", "", $str);
    $str = str_replace("</script>", "", $str);
    $str = str_replace("select","",$str);
    $str = str_replace('create','',$str);
    $str = str_replace('show','',$str);
    $str = str_replace('insert','',$str);
    $str = str_replace('update','',$str);
    $str = str_replace('delete','',$str);
    $str = str_replace('union','',$str);
    $str = str_replace('into','',$str);
    $str = str_replace('load_file','',$str);
    $str = str_replace('outfile','',$str);
    $str = str_replace("join","",$str);
    $str = str_replace("union","",$str);
    $str = str_replace("where","",$str);
    $str = str_replace("insert","",$str);
    $str = str_replace("delete","",$str);
    $str = str_replace("update","",$str);
    $str = str_replace("like","",$str);
    $str = str_replace("drop","",$str);
    $str = str_replace("DROP","",$str);
    $str = str_replace("modify","",$str);
    $str = str_replace("rename","",$str);
    $str = str_replace("alter","",$str);
    $str = str_replace("cas","",$str);
    $str = str_replace("&","",$str);
    $str = str_replace(">","",$str);
    $str = str_replace("<","",$str);
    $str = str_replace(" ",chr(32),$str);
    $str = str_replace(" ",chr(9),$str);
    $str = str_replace("    ",chr(9),$str);
    $str = str_replace("&",chr(34),$str);
    $str = str_replace("'",chr(39),$str);
    $str = str_replace("<br />",chr(13),$str);
    $str = str_replace("''","'",$str);
    $str = str_replace("css","'",$str);
    $str = str_replace("CSS","'",$str);
    $str = str_replace("<!--","",$str);
    $str = str_replace("convert","",$str);
    $str = str_replace("md5","",$str);
    $str = str_replace("passwd","",$str);
    $str = str_replace("password","",$str);
    $str = str_replace("../","",$str);
    $str = str_replace("./","",$str);
    $str = str_replace("Array","",$str);
    $str = str_replace("or 1='1'","",$str);
    $str = str_replace(";set|set&set;","",$str);
    $str = str_replace("`set|set&set`","",$str);
    $str = str_replace("--","",$str);
    $str = str_replace("OR","",$str);
    $str = str_replace('"',"",$str);
    $str = str_replace("*","",$str);
    $str = str_replace("-","",$str);
    $str = str_replace("+","",$str);
    $str = str_replace("/","",$str);
    $str = str_replace("=","",$str);
    $str = str_replace("'/","",$str);
    $str = str_replace("-- ","",$str);
    $str = str_replace(" -- ","",$str);
    $str = str_replace(" --","",$str);
    $str = str_replace("(","",$str);
    $str = str_replace(")","",$str);
    $str = str_replace("{","",$str);
    $str = str_replace("}","",$str);
    // $str = str_replace("-1","",$str);
    // $str = str_replace("1","",$str);
    $str = str_replace(".","",$str);
    $str = str_replace("response","",$str);
    $str = str_replace("write","",$str);
    $str = str_replace("|","",$str);
    $str = str_replace("`","",$str);
    $str = str_replace(";","",$str);
    $str = str_replace("etc","",$str);
    $str = str_replace("root","",$str);
    $str = str_replace("//","",$str);
    $str = str_replace("!=","",$str);
    $str = str_replace("$","",$str);
    $str = str_replace("&","",$str);
    $str = str_replace("&&","",$str);
    $str = str_replace("==","",$str);
    $str = str_replace("#","",$str);
    // $str = str_replace("@","",$str);
    $str = str_replace("mailto:","",$str);
    $str = str_replace("CHAR","",$str);
    $str = str_replace("char","",$str);
    $str = str_replace('%20','',$str);
    $str = str_replace('%27','',$str);
    $str = str_replace('*','',$str);
    $str = str_replace("\"",'',$str);
    $str = str_replace('&amp;','&',$str);
    $str = str_replace('&nbsp;',' ',$str);
    $str = str_replace(';','',$str);
    $str = str_replace('"','&quot;',$str);
    $str = str_replace("'",'&#039;',$str);
    $str = str_replace("<","&lt;",$str);
    $str = str_replace(">","&gt;",$str);
    $str = str_replace('(','',$str);
    $str = str_replace(')','',$str);
    $str = str_replace("{",'',$str);
    $str = str_replace('}','',$str);
    $str = str_replace("\t","   &nbsp;  &nbsp;",$str);
    $str = str_replace("\r","",$str);
    $str = str_replace("   "," &nbsp; ",$str);

    return $str;
}

function filteMinTest($str){
    if (empty($str)) return '';
    $str = htmlspecialchars($str);
//    $str = str_replace( '/', "", $str);
    $str = str_replace( '"', "", $str);
    $str = str_replace( '(', "", $str);
    $str = str_replace( ')', "", $str);
    $str = str_replace( 'CR', "", $str);
    $str = str_replace( 'ASCII', "", $str);
    $str = str_replace( 'ASCII 0x0d', "", $str);
    $str = str_replace( 'LF', "", $str);
    $str = str_replace( 'ASCII 0x0a', "", $str);
    $str = str_replace( ',', "", $str);
    $str = str_replace( '%', "", $str);
    $str = str_replace( ';', "", $str);
    $str = str_replace( 'eval', "", $str);
    $str = str_replace( 'open', "", $str);
    $str = str_replace( 'sysopen', "", $str);
    $str = str_replace( 'system', "", $str);
    $str = str_replace( '$', "", $str);
    $str = str_replace( "'", "", $str);
    $str = str_replace( "'", "", $str);
    $str = str_replace( 'ASCII 0x08', "", $str);
    $str = str_replace( '"', "", $str);
    $str = str_replace( '"', "", $str);
    $str = str_replace("", "", $str);
    $str = str_replace("&gt", "", $str);
    $str = str_replace("&lt", "", $str);
    $str = str_replace("<SCRIPT>", "", $str);
    $str = str_replace("</SCRIPT>", "", $str);
    $str = str_replace("<script>", "", $str);
    $str = str_replace("</script>", "", $str);
    $str = str_replace("select","",$str);
    $str = str_replace('create','',$str);
    $str = str_replace('show','',$str);
    $str = str_replace('insert','',$str);
    $str = str_replace('update','',$str);
    $str = str_replace('delete','',$str);
    $str = str_replace('union','',$str);
    $str = str_replace('into','',$str);
    $str = str_replace('load_file','',$str);
    $str = str_replace('outfile','',$str);
    $str = str_replace("join","",$str);
    $str = str_replace("union","",$str);
    $str = str_replace("where","",$str);
    $str = str_replace("insert","",$str);
    $str = str_replace("delete","",$str);
    $str = str_replace("update","",$str);
    $str = str_replace("like","",$str);
    $str = str_replace("drop","",$str);
    $str = str_replace("DROP","",$str);
    $str = str_replace("modify","",$str);
    $str = str_replace("rename","",$str);
    $str = str_replace("alter","",$str);
    $str = str_replace("cas","",$str);
    $str = str_replace("&","",$str);
    $str = str_replace(">","",$str);
    $str = str_replace("<","",$str);
    $str = str_replace(" ",chr(32),$str);
    $str = str_replace(" ",chr(9),$str);
    $str = str_replace("    ",chr(9),$str);
    $str = str_replace("&",chr(34),$str);
    $str = str_replace("'",chr(39),$str);
    $str = str_replace("<br />",chr(13),$str);
    $str = str_replace("''","'",$str);
    $str = str_replace("css","'",$str);
    $str = str_replace("CSS","'",$str);
    $str = str_replace("<!--","",$str);
    $str = str_replace("convert","",$str);
    $str = str_replace("md5","",$str);
    $str = str_replace("passwd","",$str);
    $str = str_replace("password","",$str);
    $str = str_replace("../","",$str);
    $str = str_replace("./","",$str);
    $str = str_replace("Array","",$str);
    $str = str_replace("or 1='1'","",$str);
    $str = str_replace(";set|set&set;","",$str);
    $str = str_replace("`set|set&set`","",$str);
    $str = str_replace("--","",$str);
    $str = str_replace("OR","",$str);
    $str = str_replace('"',"",$str);
    $str = str_replace("*","",$str);
//    $str = str_replace("-","",$str);
    $str = str_replace("+","",$str);
//    $str = str_replace("/","",$str);
    $str = str_replace("=","",$str);
    $str = str_replace("'/","",$str);
    $str = str_replace("-- ","",$str);
    $str = str_replace(" -- ","",$str);
    $str = str_replace(" --","",$str);
    $str = str_replace("(","",$str);
    $str = str_replace(")","",$str);
    $str = str_replace("{","",$str);
    $str = str_replace("}","",$str);
    // $str = str_replace("-1","",$str);
    // $str = str_replace("1","",$str);
//    $str = str_replace(".","",$str);
    $str = str_replace("response","",$str);
    $str = str_replace("write","",$str);
    $str = str_replace("|","",$str);
    $str = str_replace("`","",$str);
    $str = str_replace(";","",$str);
    $str = str_replace("etc","",$str);
    $str = str_replace("root","",$str);
//    $str = str_replace("//","",$str);
    $str = str_replace("!=","",$str);
    $str = str_replace("$","",$str);
    $str = str_replace("&","",$str);
    $str = str_replace("&&","",$str);
    $str = str_replace("==","",$str);
    $str = str_replace("#","",$str);
    // $str = str_replace("@","",$str);
    $str = str_replace("mailto:","",$str);
    $str = str_replace("CHAR","",$str);
    $str = str_replace("char","",$str);
    $str = str_replace('%20','',$str);
    $str = str_replace('%27','',$str);
    $str = str_replace('*','',$str);
    $str = str_replace("\"",'',$str);
    $str = str_replace('&amp;','&',$str);
    $str = str_replace('&nbsp;',' ',$str);
    $str = str_replace(';','',$str);
    $str = str_replace('"','&quot;',$str);
    $str = str_replace("'",'&#039;',$str);
    $str = str_replace("<","&lt;",$str);
    $str = str_replace(">","&gt;",$str);
    $str = str_replace('(','',$str);
    $str = str_replace(')','',$str);
    $str = str_replace("{",'',$str);
    $str = str_replace('}','',$str);
    $str = str_replace("\t","   &nbsp;  &nbsp;",$str);
    $str = str_replace("\r","",$str);
    $str = str_replace("   "," &nbsp; ",$str);

    return $str;
}

// 获取毫秒
function msectime() {
    list($msec, $sec) = explode(' ', microtime());
    return (float)sprintf('%.0f', (floatval($msec) + floatval($sec)) * 1000);
}

// 生成二维码
function qrcode($url=''){
    Vendor('phpqrcode.phpqrcode');
    //生成二维码图片
    $object = new QRcode();
    //$url='cehi';//网址或者是文本内容
    $level=3;
    $size=4;
    $errorCorrectionLevel =intval($level) ;//容错级别
    $matrixPointSize = intval($size);//生成图片大小
    $object->png($url, false, $errorCorrectionLevel, $matrixPointSize, 2);
}

// 生成二维码,并保存
function qrcodes($url='',$name){
    $path = 'Uploads/Qrcode';
    if (!is_dir($path)) {
        mkdir($path, 0777, true);
    }
    Vendor('phpqrcode.phpqrcode');
    //生成二维码图片
    $level=3;
    $size=4;
    $errorCorrectionLevel =intval($level) ;//容错级别
    $matrixPointSize = intval($size);//生成图片大小
    $save_path = $path . '/' . $name . '01.png';
    QRcode::png($url, $save_path, $errorCorrectionLevel, $matrixPointSize, 2);
//添加背景图
    $bg_img = "./Public/share/images/bg_img.png";
    $image_1 = imagecreatefromstring(file_get_contents($bg_img));
    $image_2 = imagecreatefromstring(file_get_contents($save_path));
    $image_3 = imageCreatetruecolor(imagesx($image_1),imagesy($image_1));
    $color = imagecolorallocate($image_3, 255, 255, 255);
    imagefill($image_3, 0, 0, $color);
    imageColorTransparent($image_3, $color);
    imagecopyresampled($image_3, $image_1, 0, 0, 0, 0, imagesx($image_1), imagesy($image_1), imagesx($image_1), imagesy($image_1));

    imagecopymerge($image_3, $image_2, 125, 325, 0, 0, imagesx($image_2), imagesy($image_2), 100);
    $path = './'.$path . '/' . $name . '.png';
    imagepng($image_3,$path);
// var_dump(imagepng($image_3,$path));die();
}

//下载
function download($file){
    if ( file_exists ( $file )) {
        header ( 'Content-Description: File Transfer' );
        header ( 'Content-Type: application/octet-stream' );
        header ( 'Content-Disposition: attachment; filename=' . basename ( $file ));
        header ( 'Content-Transfer-Encoding: binary' );
        header ( 'Expires: 0' );
        header ( 'Cache-Control: must-revalidate' );
        header ( 'Pragma: public' );
        header ( 'Content-Length: ' . filesize ( $file ));
        ob_clean ();
        flush ();
        readfile ( $file );
        exit;
    }
}